using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Utilities;
using System.Web;

namespace MP.SharePoint.Workflow
{
    class WFReceiver : SPItemEventReceiver
    {
        void StartWorkflowAnon(SPItemEventProperties properties)
        {
             SPSecurity.RunWithElevatedPrivileges(delegate()
                {
            using(SPSite site = new SPSite(properties.WebUrl))
            using (SPWeb web = site.OpenWeb())
            {
               
                    web.AllowUnsafeUpdates = true;

                    SPList list = web.Lists[properties.ListId];

                  

                    Guid wfId = new Guid(web.Properties[list.ID.ToString() + ";" + properties.EventType.ToString()]);
               SPWorkflowAssociation assoc = list.WorkflowAssociations[wfId];


                    site.WorkflowManager.StartWorkflow(properties.ListItem, assoc, assoc.AssociationData);

                    web.AllowUnsafeUpdates = false;
             

            }

        });
        }



        public override void ItemAdded(SPItemEventProperties properties)
        {
            


            base.ItemAdded(properties);

            StartWorkflowAnon(properties);

        }

        public override void ItemAttachmentAdded(SPItemEventProperties properties)
        {
            base.ItemAttachmentAdded(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemAttachmentAdding(SPItemEventProperties properties)
        {
            base.ItemAttachmentAdding(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemAdding(SPItemEventProperties properties)
        {
            base.ItemAdding(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemAttachmentDeleted(SPItemEventProperties properties)
        {
            base.ItemAttachmentDeleted(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemAttachmentDeleting(SPItemEventProperties properties)
        {
            base.ItemAttachmentDeleting(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemCheckedIn(SPItemEventProperties properties)
        {
            base.ItemCheckedIn(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemCheckedOut(SPItemEventProperties properties)
        {
            base.ItemCheckedOut(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemCheckingIn(SPItemEventProperties properties)
        {
            base.ItemCheckingIn(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemCheckingOut(SPItemEventProperties properties)
        {
            base.ItemCheckingOut(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemDeleted(SPItemEventProperties properties)
        {
            base.ItemDeleted(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemDeleting(SPItemEventProperties properties)
        {
            base.ItemDeleting(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemFileConverted(SPItemEventProperties properties)
        {
            base.ItemFileConverted(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemFileMoved(SPItemEventProperties properties)
        {
            base.ItemFileMoved(properties);

            StartWorkflowAnon(properties);
        }

        


        public override void ItemFileMoving(SPItemEventProperties properties)
        {
         
            base.ItemFileMoving(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemUncheckedOut(SPItemEventProperties properties)
        {
            base.ItemUncheckedOut(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemUncheckingOut(SPItemEventProperties properties)
        {
            base.ItemUncheckingOut(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemUpdated(SPItemEventProperties properties)
        {
            base.ItemUpdated(properties);

            StartWorkflowAnon(properties);
        }

        public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);

            StartWorkflowAnon(properties);
        }
    }
}
